package com.frank.loadinglibrary.common.gear;

import com.frank.loadinglibrary.common.BaseLoadingComponent;
import ohos.agp.components.AttrSet;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

public class GearComponent extends BaseLoadingComponent {

    private Paint outerPaint = new Paint();
    private Paint innerPaint = new Paint();
    private Paint gearPaint = new Paint();
    private String teethColor = "#efefef";
    private String outerCircleColor = "#f3f707";
    private String innerCircleColor = "#ffffff";
    private float innerCircleRadiusPercent = 4 / 15f;
    private float outerCircleRadiusPercent = 2 / 5f;
    private int teethCount = 10;
    private int teethTopWith = 10;

    public GearComponent setTeethCount(int teethCount) {
        this.teethCount = teethCount;
        return this;
    }

    public GearComponent setTeethColor(String teethColor) {
        this.teethColor = teethColor;
        return this;
    }

    public GearComponent setOuterCircleColor(String outerCircleColor) {
        this.outerCircleColor = outerCircleColor;
        return this;
    }

    public GearComponent setTeethTopWith(int teethTopWith) {
        this.teethTopWith = teethTopWith;
        return this;
    }


    public GearComponent setInnerCircleColor(String innerCircleColor) {
        this.innerCircleColor = innerCircleColor;
        return this;
    }

    public GearComponent setInnerCircleRadiusPercent(float innerCircleRadiusPercent) {
        this.innerCircleRadiusPercent = innerCircleRadiusPercent;
        return this;
    }

    public GearComponent setOuterCircleRadiusPercent(float outerCircleRadiusPercent) {
        this.outerCircleRadiusPercent = outerCircleRadiusPercent;
        return this;
    }

    public GearComponent(Context context) {
        this(context, null);
    }

    public GearComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public GearComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initUI();
    }

    @Override
    protected void stopAnimators() {

    }

    @Override
    protected void startAnimators() {

    }

    private void initUI() {
        setupPaint();
        addDrawTask((component, canvas) -> {
            setPaintColor();
            drawTeeth(canvas);
        });
    }

    private void drawTeeth(Canvas canvas) {
        float outerCircleRadius = getWidth() * outerCircleRadiusPercent;
        float innerCircleRadius = getWidth() * innerCircleRadiusPercent;
        int centerPoint = getWidth() / 2;
        canvas.save();
        Path path = new Path();
        path.moveTo(0, centerPoint - teethTopWith / 2f);
        path.lineTo(0, centerPoint + teethTopWith / 2f);
        double degree = Math.PI / (teethCount * 2);
        double xC = centerPoint - outerCircleRadius * Math.cos(degree);
        path.lineTo((float) xC, (float) (centerPoint + outerCircleRadius * Math.sin(degree)));
        path.lineTo((float) xC, (float) (centerPoint - outerCircleRadius * Math.sin(degree)));
        path.close();
        canvas.drawPath(path, gearPaint);
        canvas.restore();

        int interval = 360 / teethCount;
        for (int angle = interval; angle < 360; angle += interval) {
            canvas.save();
            canvas.rotate(angle, centerPoint, centerPoint);
            canvas.drawPath(path, gearPaint);
            canvas.restore();
        }

        canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, outerCircleRadius, outerPaint);

        canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, innerCircleRadius, innerPaint);
    }

    private void setPaintColor() {
        outerPaint.setColor(new Color(Color.getIntColor(outerCircleColor)));

        innerPaint.setColor(new Color(Color.getIntColor(innerCircleColor)));

        gearPaint.setColor(new Color(Color.getIntColor(teethColor)));
    }

    private void setupPaint() {
        outerPaint.setAntiAlias(true);
        outerPaint.setStyle(Paint.Style.FILL_STYLE);

        innerPaint.setAntiAlias(true);
        innerPaint.setStyle(Paint.Style.FILL_STYLE);

        gearPaint.setStyle(Paint.Style.FILL_STYLE);
        gearPaint.setAntiAlias(true);
    }
}
